import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'

import NProgress from 'nprogress'

import 'nprogress/nprogress.css'
NProgress.configure({ showSpinner: false })
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  NProgress.start();
  document.title = to.meta && to.meta.title || '饿了吗'


  let token = localStorage.getItem('token')


  if (to.path === '/ORders' || to.path === '/Mine') {
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next();
  }
})


export default router
